前面提到了很多 DX 與 Shader 的設定,但現在遊戲的畫面越來越精緻,不可能每一幀都只計算一個 function。
所以這時 GPU 最重要的能力就出來了,就是可以同時間平行計算的能力。
如果有往下翻 Microsoft 介紹 pipline 的資料,會發現除了本來的 Graphics pipeline,還有一個叫 Compute pipeline 的介紹圖。

基本上大部分的設定跟前面相似,這裡直接展示程式。
//pipeline_state.cpp
void PipelineState::init(D3D12_COMPUTE_PIPELINE_STATE_DESC desc)
{
if (g_graphicsEngine == nullptr || g_graphicsEngine->getD3DDevice() == nullptr)
throw std::runtime_error("PipelineState: Graphics device is not initialized.");
auto d3dDevice = g_graphicsEngine->getD3DDevice();
auto hr = d3dDevice->CreateComputePipelineState(&desc, IID_PPV_ARGS(&m_pipelineState));
if (FAILED(hr))
{
throw std::runtime_error("PipelineState: Failed to create the compute pipeline state.");
}
}
//render_context.h
class RenderContext
{
public:
...
// 使用 RootSignature 包裝類別設定 Compute Root Signature
void setComputeRootSignature(RootSignature& rootSignature)
{
m_commandList->SetComputeRootSignature(rootSignature.get());
}
// 將 Constant Buffer 的 GPU 位址設定到指定的 Compute Root Parameter
void setComputeRootConstantBufferView(UINT rootParameterIndex, D3D12_GPU_VIRTUAL_ADDRESS address)
{
m_commandList->SetComputeRootConstantBufferView(rootParameterIndex, address);
}
// 將 Descriptor Heap 中的 GPU Handle 綁定到指定的 Compute Descriptor Table
void setComputeRootDescriptorTable(UINT rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE handle)
{
m_commandList->SetComputeRootDescriptorTable(rootParameterIndex, handle);
}
// Dispatch 指定數量的 Compute Shader Thread Group
void dispatch(UINT x, UINT y, UINT z)
{
m_commandList->Dispatch(x, y, z);
}
...
}
Pipelines and Shaders with Direct3D 12 - Win32 apps